package pab.core.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pab.core.transaction.Currency;

public class FixedExchangeRate implements ExchangeRate {

	private static final List<Currency> SUPPORTED_CURRENCIES = new ArrayList<Currency>();
	private static final double[][] RATE_MATRIX = new double[3][3];
	
	private static final Map<String, Currency> CURRENCY_MAP = new HashMap<String, Currency>();
	static {
		Currency curreny = new Currency("VND", "Viet Nam Dong");
		SUPPORTED_CURRENCIES.add(curreny);
		CURRENCY_MAP.put(curreny.getCode(), curreny);
		curreny = new Currency("USD", "US Dollard");
		SUPPORTED_CURRENCIES.add(curreny);
		CURRENCY_MAP.put(curreny.getCode(), curreny);
		curreny = new Currency("EUR", "Euro");
		SUPPORTED_CURRENCIES.add(curreny);
		CURRENCY_MAP.put(curreny.getCode(), curreny);
		
		int vIndex = 0;
		int uIndex = 1;
		int eIndex = 2;
		RATE_MATRIX[vIndex][vIndex] = 1;
		RATE_MATRIX[vIndex][uIndex] = 1.0 / 20570.0;
		RATE_MATRIX[vIndex][eIndex] = 1.0 / 29512.8066;
		RATE_MATRIX[uIndex][vIndex] = 20570.0;
		RATE_MATRIX[uIndex][uIndex] = 1;
		RATE_MATRIX[uIndex][eIndex] = 0.697;
		RATE_MATRIX[eIndex][vIndex] = 29512.8066;
		RATE_MATRIX[eIndex][uIndex] = 1.4347;
		RATE_MATRIX[eIndex][eIndex] = 1;
		
		
	}
	public double exchangeRate(Currency fromCurreny, Currency toCurrency) {
		int fromIndex = getIndex(fromCurreny);
		int toIndex = getIndex(toCurrency);
		return RATE_MATRIX[fromIndex][toIndex];
	}

	public List<Currency> getSupportedCurrencies() {
		return SUPPORTED_CURRENCIES;
	}
	
	public Currency getCurrency(String code) {
		return CURRENCY_MAP.get(code);
	}
	private int getIndex(Currency currency) {
		if ("VND".equals(currency.getCode())) return 0;
		else if ("USD".equals(currency.getCode())) return 1;
		else if ("EUR".equals(currency.getCode())) return 2;
		else return -1;
	}

}
